home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / FREC.C < prev    next >
C/C++ Source or Header  |  1993-08-25  |  7KB  |  242 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    app.cpp
  5. //   Title:    Application Template
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains the program entry point for
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <base.h>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //    Stack size
  45. //----------------------------------------------------------------------------
  46. #if COMPILER_BORLAND && (OS_DOS || OS_WINDOWS)
  47. unsigned _stklen = 0x4000;
  48. #endif
  49.  
  50.  
  51. //----------------------------------------------------------------------------
  52. //    Globals
  53. //----------------------------------------------------------------------------
  54.  
  55. //
  56. // A useless string which is simply embedded in the executable.
  57. //
  58. PSZ __pszCredits__ = "Written by John M Weeder, 1993";
  59.  
  60.  
  61. //
  62. //    This should contain a program description which will be displayed as
  63. //    part of the program's help text.
  64. //
  65. static PCSZ pcszDescription =
  66.     "This program extracts records from a fixed length record data file.";
  67.  
  68. //
  69. //    Program command line options
  70. //
  71. #if OS_UNIX
  72. #define MAX_BUFFER    ((SIZET)(1024 * 1024 * 4))
  73. #else
  74. #define MAX_BUFFER    ((SIZET)0xF000)
  75. #endif
  76.  
  77. static LONG lRecSize = 0;
  78. static LONG lStart = 1;
  79. static LONG lEnd = -1;
  80. static LONG lLength = -1;
  81. static LONG lHeader = 0;
  82. static LONG lSkip = 0;
  83. static BOOL fAppend = FALSE;
  84. static CHAR szInput[MAX_PATH];
  85. static CHAR szOutput[MAX_PATH];
  86. static HF hfInput = -1;
  87. static HF hfOutput = -1;
  88. static PBYTE pb = NULL;
  89. static BS_CMDOPT acmdopt[] =
  90.     {
  91.     { "APPEND",    (PVOID)&fAppend,         CMDOPT_TRUE,              "Append to output file?"},
  92.     { "BEGIN",        (PVOID)&lStart,         CMDOPT_NUMERIC,          "First record to process."},
  93.     { "END",        (PVOID)&lEnd,             CMDOPT_NUMERIC,          "Last record to process."},
  94.     { "HEADER",    (PVOID)&lHeader,         CMDOPT_NUMERIC,          "Size of header in bytes."},
  95.     { "LENGTH",    (PVOID)&lLength,         CMDOPT_NUMERIC,          "Number of records to process."},
  96.     { "RECSIZE",   (PVOID)&lRecSize,        CMDOPT_NUMERIC,          "Size of record in bytes."},
  97.     { "SKIP",        (PVOID)&lSkip,         CMDOPT_NUMERIC,            "Number of records to skip."},
  98.     { "input",         (PVOID)szInput,         CMDOPT_FILESPECR(80),    "Input file name."},
  99.     { "output",     (PVOID)szOutput,         CMDOPT_FILESPECR(80),    "Output file name."},
  100.     BS_CMDOPT_NULL,
  101.     };
  102.  
  103.  
  104. //----------------------------------------------------------------------------
  105. //   Description:    
  106. //    Parameters:
  107. //       Returns:    TRUE if successful.
  108. //----------------------------------------------------------------------------
  109. static SHORT FN_L Process(void)
  110. {
  111.     FLAG16 fsInput = FL_OPEN|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  112.     FLAG16 fsOutput = FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  113.     PCSZ pcszData = EnvGet("DATA");
  114.     FPOS fsize;
  115.     FPOS fpos;
  116.     LONG lRecords;
  117.     SIZET cBuf;
  118.  
  119.     if (lRecSize <= 0)                        // Check record size
  120.         {
  121.         Output("Invalid records size specified.\n");
  122.         return 95;
  123.         }
  124.     if (lRecSize > 8192)                        // Set default parameter values
  125.         lRecSize = 8192;
  126.     if (lStart < 1)
  127.         lStart = 1;
  128.     if (lSkip < 0)
  129.         lSkip = 0;
  130.                                                     // Open input file
  131.     if (!FnameQualify(szInput, NULL, pcszData, 0))
  132.         return 99;
  133.     if (!FileOpen(&hfInput, szInput, fsInput, NULL))
  134.         return 98;
  135.     Output("Opened input file '%s'\n", szInput);
  136.                                                     // Open output file
  137.     if (!FnameQualify(szOutput, NULL, pcszData, 0))
  138.         return 97;
  139.     if (!fAppend)
  140.         fsOutput |= (FL_CREATE|FL_TRUNCATE);
  141.     else
  142.         fsOutput |= (FL_CREATE|FL_OPEN);;
  143.     if (!FileOpen(&hfOutput, szOutput, fsOutput, NULL))
  144.         return 96;
  145.     Output("Opened output file '%s'\n", szOutput);
  146.  
  147.     fsize = FileGetSize(hfInput);            // Get input file size
  148.     if (fsize < 0)
  149.         return 91;
  150.     fsize = MAX(fsize - lHeader, 0);
  151.     if (lRecSize > fsize)
  152.         lRecSize = fsize;
  153.     fpos = FileGetSize(hfOutput);            // Get output file size
  154.     if (fpos < 0)
  155.         return 90;
  156.     lRecords = fsize / lRecSize;
  157.     if ((fsize % lRecSize) > 1)            // Verify integral number of records
  158.         {                                            // Extra byte allowed is for Ctrl-Z
  159.         Output("%ld extra bytes ignored.\n", (fsize % lRecSize));
  160.         }
  161.     if (lRecords == 0)
  162.         {
  163.         Output("No records found in input file.\n");
  164.         return 0;
  165.         }
  166.     if (lStart > lRecords)
  167.         {
  168.         Output("No records copied.\n");
  169.         return 0;
  170.         }
  171.     if (lLength > 0)
  172.         lEnd = lStart + lLength - 1;
  173.     if (lEnd > lRecords || lEnd < lStart)
  174.         lEnd = lRecords;
  175.     if (lSkip)
  176.         cBuf = 1;
  177.     else
  178.         cBuf = MAX_BUFFER / (SIZET)lRecSize;
  179.     pb = MemAlloc(cBuf * (SIZET)lRecSize);
  180.     if (pb == NULL)
  181.         {
  182.         Output("Unable to allocate copy buffer.\n");
  183.         return 94;
  184.         }
  185.     Output("Record size is %ld bytes.\n", lRecSize);
  186.     Output("Copying records %ld to %ld", lStart, lEnd);
  187.     if (lSkip)
  188.         Output(", every %ld%s record", lSkip+1, strord(lSkip+1));
  189.     Output(".\n");
  190.     while (lStart <= lEnd)
  191.         {
  192.         SIZET cCopy = (SIZET)MIN((LONG)cBuf, (lEnd - lStart + 1));
  193.         SIZET cCopyBytes = cCopy * (SIZET)lRecSize;
  194.  
  195.         Output("\r%08ld", lStart);
  196.         if (!FileRead(hfInput, pb, cCopyBytes, ((lStart - 1) * lRecSize + lHeader)))
  197.             return 93;
  198.         if (!FileWrite(hfOutput, pb, cCopyBytes, fpos))
  199.             return 92;
  200.         fpos += (FPOS)cCopyBytes;
  201.         lStart += (LONG)cCopy;
  202.         lStart += lSkip;
  203.         }
  204.     Output("\r%08ld\n", lEnd);
  205.     return 0;
  206. }
  207.  
  208.  
  209. //----------------------------------------------------------------------------
  210. //   Description:    main() - Program entry point
  211. //    Parameters:    Standard C parameters
  212. //       Returns:    DOS return code.
  213. //----------------------------------------------------------------------------
  214. int main(int argc, char **argv)
  215. {
  216. static BS_CFG cfg = CFG_DFT;
  217.     SHORT sResult;
  218.  
  219.     //
  220.     //    Initialize base library
  221.     //
  222.     BaseLibraryInitialize(argc, argv, &cfg);
  223.     BaseTitle("$Revision:  93.1  $", __DATE__, __TIME__, "Fixed Length Record File Utility");
  224.     if (!BaseTitleHelp(acmdopt, pcszDescription, 0))
  225.         return 99;
  226.  
  227.     sResult = Process();
  228.     Output(sResult ? "Failed.\n": "Success.\n");
  229.     if (hfInput >= 0)
  230.         FileClose(hfInput);
  231.     if (hfOutput >= 0)
  232.         FileClose(hfOutput);
  233.     if (pb)
  234.         MemFree(pb);
  235.     return sResult;
  236. }
  237. //----------------------------------------------------------------------------
  238. //------------------------------- End of File --------------------------------
  239. //----------------------------------------------------------------------------
  240.  
  241.  
  242.